package com.guodi.map.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.guodi.common.constant.StatusCode;
import com.guodi.common.utils.UuidUtil;
import com.guodi.core.boot.ctrl.BaseController;
import com.guodi.core.minio.util.MinioUtil;
import com.guodi.core.secure.utils.AuthUtil;
import com.guodi.core.tool.api.R;
import com.guodi.map.entity.MapMyMap;
import com.guodi.map.service.IMapMyMapService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import springfox.documentation.annotations.ApiIgnore;

import javax.annotation.Resource;
import java.io.IOException;
import java.util.Date;

/**
 * @作者: 林楷
 * @日期: 2021/11/18
 * @描述: 我的地图
 */
@RestController
@RequestMapping("/MapMyMap")
@Api(value = "API - MapMyMapController", tags = "我的地图")
public class MapMyMapController extends BaseController {

    @Resource
    private IMapMyMapService myMapService;

    /**
     * @作者: 林楷 2021/11/18
     * @描述: 根据分页查询
     * @参数:
     * @返回:
     */
    @PostMapping("/list")
    @ApiOperation(value = "根据分页查询")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "current", value = "当前页索引", required = true, dataType = "long", paramType = "query"),
            @ApiImplicitParam(name = "size", value = "页面记录大小", required = true, dataType = "long", paramType = "query"),
            @ApiImplicitParam(name = "keyWord", value = "关键字搜索", dataType = "String", paramType = "query")
    })
    public R listByEntity(@ApiIgnore Page page, String keyWord){
        LambdaQueryWrapper<MapMyMap> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(MapMyMap::getCreator, String.valueOf(AuthUtil.getUserId()));
        if(StringUtils.isNotBlank(keyWord)) {
            queryWrapper.like(MapMyMap::getName, keyWord);
        }
        queryWrapper.orderByDesc(MapMyMap::getCreateTime);
        return R.data(myMapService.page(page, queryWrapper));
    }
    /**
     * @作者: 林楷 2021/11/18
     * @描述: 保存
     * @参数:
     * @返回:
     */
    @PostMapping("/save")
    @ApiOperation(value = "保存")
    public R save(MapMyMap mapMyMap){
        try {
            mapMyMap.setId(UuidUtil.get32UUID());
            mapMyMap.setCreator(String.valueOf(AuthUtil.getUserId()));
            mapMyMap.setCreateTime(new Date());
            if(myMapService.add(mapMyMap)) {
                return R.data(mapMyMap);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        return R.data(StatusCode.REQUEST_CODE_OPERATION_FAILURE, "保存失败");
    }

    /**
     * @作者: 林楷 2021/11/18
     * @描述: 删除
     * @参数:
     * @返回:
     */
    @PostMapping("/delete")
    @ApiOperation(value = "删除")
    @ApiImplicitParam(name = "id", value = "主键", required = true, dataType = "string", paramType = "query")
    public R delete(String id){
        if(StringUtils.isBlank(id)) {
            return R.data(StatusCode.REQUEST_CODE_OPERATION_FAILURE, "ID不为空");
        }
        MapMyMap myMap = myMapService.getById(id);
        if(myMap != null) {
            MinioUtil.removeFile(myMap.getPath());
            myMapService.removeById(id);
            return R.status(true);
        }
        return R.data(StatusCode.REQUEST_CODE_OPERATION_FAILURE, "保存失败");
    }
}
